package java67_adv9;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

//聊天的线程
public class SockThread extends Thread {
	private Socket s;

	public SockThread(Socket s) {
		this.s = s;
		super.setName(String.valueOf(s.getInetAddress()));
	}

	@Override
	public void run() {
		System.out.println(s.getInetAddress() + ":" + s.getPort() + "连接上了...");
		try {
			InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			PrintWriter pw = new PrintWriter(out, true);
			String s = null;
			while (true) {
				s = br.readLine();
				if (s == null || s.length() == 0)
					continue;
				// System.out.println(s);
				pw.println("你说的事:[" + s + "]朕知道了,跪安吧");
				if ("quit".equalsIgnoreCase(s))
					break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (s != null)
				try {
					s.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
}
